from django.http import HttpRequest
from django.utils.deprecation import MiddlewareMixin


class TestMiddleWare(MiddlewareMixin):

    def process_request(self, request: HttpRequest):
        print('111每次请求前都会调用执行')
        # username = request.COOKIES.get('name')
        # if username is None:
        #     print('无用户信息')
        # else:
        #     print(f'已登录,username:{username}')

    def process_response(self, request: HttpRequest, response):
        print('111每次响应前都会调用执行')
        return response


class TestMiddleWare2(MiddlewareMixin):

    def process_request(self, request: HttpRequest):
        print('222每次请求前都会调用执行')
        username = request.COOKIES.get('name')
        if username is None:
            print('无用户信息')
        else:
            print(f'已登录,username:{username}')

    def process_response(self, request: HttpRequest, response):
        print('222每次响应前都会调用执行')
        return response
